package cn.zhaoyuening.schoolmates.web.general;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.zhaoyuening.schoolmates.domain.Photos;
import cn.zhaoyuening.schoolmates.service.PhotosService;
import cn.zhaoyuening.schoolmates.utils.JsonUtils;
import cn.zhaoyuening.schoolmates.utils.page.PageBean;

@RestController
public class PhotosController {
	@Autowired
	private PhotosService photosService;
	

	
	@RequestMapping(value="photos",method=RequestMethod.GET,produces="application/json;charset=utf-8")
	public String findByCid(@RequestParam(defaultValue="-1") Long cid,@RequestParam(defaultValue="desc") String order,
			@RequestParam(defaultValue="0") Integer offset, @RequestParam(defaultValue="10") Integer limit){
		List<Photos> list = null;
		Long total = photosService.getTotalRecord();
		//如果cid为-1 获取所有栏目相册
		if(cid==-1){
			list = photosService.findAll(null, null, offset, limit);
		}else{
			list = photosService.findAll("cid = ?", new Object[]{cid}, offset, limit, "id "+order);			
		}
		PageBean<Photos> pageBean = new PageBean<>(total,list);
		String json = JsonUtils.objectToJson(pageBean);
		return json;
	}
	

	
}
